﻿Public Class frmCaregivers
    Private m_cItems As New Collection
    Sub Populate()
        For Each dude As CareGiver In ProviList
            lstImages.Images.Add(dude.ID, dude.Photo)
            Dim r As New ListViewItem(String.Format("{0} {1}", dude.FirstName, dude.LastName))

            r.ImageIndex = dude.ID
            r.ImageKey = dude.ID
            r.Tag = dude

            m_cItems.Add(r)
            lvRes.Items.Add(r)
        Next
    End Sub
    Sub UpdateResults()
        Dim remove As Boolean
        Dim dude As CareGiver

        For Each item As ListViewItem In m_cItems
            '(Re)set the variables
            remove = False

            'Between changes in main.vb, and updating other items, it became possible
            'For item to be null here, and by extension, it is possible item.Tag
            'can be null here as well

            'If it is, just advance to the next item

            If item Is Nothing OrElse item.Tag Is Nothing Then Continue For

            dude = CType(item.Tag, CareGiver)

            'Similar to the child, except for caregiver

            'If the first name does not contain the text offered, quit
            'If the last name does not contain the text offered, quit
            'If the gender equals MALE and MALE is not checked, quit
            'If the gender equals FEMALE and FEMALE is not checked, quit
            If Not (dude.FirstName Like "*" & txtFName.Text & "*") Then remove = True
            If Not (dude.LastName Like "*" & txtLName.Text & "*") Then remove = True
            If dude.Gender.Equals(MALE) And Not chkMale.Checked Then remove = True
            If dude.Gender.Equals(FEMALE) And Not chkFemale.Checked Then remove = True

            'If not marked for removal, re-add if not in view, else remove from view

            If Not remove Then
                'Only add it if it needs re-adding
                If Not lvRes.Items.Contains(item) Then lvRes.Items.Add(item)
            Else
                item.Remove()
            End If
        Next
    End Sub
    Private Sub frmCaregivers_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
    Private Sub frmCaregivers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = Icon.FromHandle(My.Resources.building.GetHicon)
        If Not extend.CheckPermission(ACCESS_ADMINISTRATOR) Then cmnMenu_Manage.Enabled = False
        Populate()
    End Sub
    Private Sub txtFName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFName.TextChanged
        UpdateResults()
    End Sub
    Private Sub txtLName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLName.TextChanged
        UpdateResults()
    End Sub
    Private Sub txtAge_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        UpdateResults()
    End Sub
    Private Sub chkMale_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMale.CheckedChanged
        UpdateResults()
    End Sub
    Private Sub chkFemale_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFemale.CheckedChanged
        UpdateResults()
    End Sub
    Private Sub cmnMenu_ViewInformation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lvRes.SelectedItems.Count < 1 Then
            Beep()
            Exit Sub
        End If

        'My.Forms.

        'Dim f As New
    End Sub
    Private Sub cmnMenu_Manage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnMenu_Manage.Click
        If lvRes.SelectedItems.Count < 1 Then
            Beep()
            Exit Sub
        End If

        Dim f As New frmRank

        f.who = TryCast(lvRes.SelectedItems(0).Tag, CareGiver)

        f.Show()
    End Sub
End Class